<?php
namespace App\Services\WxMaterial\Factory;

use App\Models\WxMaterial;
use App\Services\WxCommon\EasyWechat;
use App\Services\SysFile\SysFileService;
use App\Services\WxCommon\WxError;
use EasyWeChat\Kernel\Messages\Music;
/**
 * 音乐素材业务逻辑
 * @package App\Services\WxMaterial
 */
class WxMaterialMusic extends \App\Services\WxCommon\WxBase implements WxMaterialInterface
{
    /**
     * 构造函数
     * @param string $appid
     * @throws \Exception
     */
    public function __construct($appid = '')
    {
        parent::__construct($appid);
    }
    /**
     * 保存素材数据
     * @param $data
     */
    public function save($data)
    {
        if (empty($data['title'])) {
            throw new \Exception('请提供音乐标题');
        }
        if (empty($data['description'])){
            throw new \Exception('请音乐音乐描述');
        }
        if (empty($data['thumb_view_url'])) {
            throw new \Exception('请音乐封面图地址');
        }
        if (empty($data['music_url'])){
            throw new \Exception('请提供音乐链接');
        }
        $materialId = $data['material_id'] ?? 0;
        if ($materialId){
            $materialModel = WxMaterial::findOrFail($materialId);
        }else{
            $materialModel = new WxMaterial;
        }
        $materialModel->appid = $this->appid;
        $materialModel->cat_id = $data['cat_id'] ?? 0;
        $materialModel->type = 'music';
        $materialModel->title = $data['title'] ?? '';
        $materialModel->description = $data['description'] ?? '';
        $materialModel->music_url = $data['music_url'] ?? '';
        $materialModel->hq_music_url = $data['hq_music_url'] ?? '';
        $materialModel->thumb_view_url = $data['thumb_view_url'] ?? '';
        $materialModel->is_forever = 0;
        if ($materialModel->thumb_view_url){//封面图上传
            $thumbService = new WxMaterialThumb();
            //永久素材上传封面图
            $thumbData = $thumbService->save([
                'title' => $materialModel->title . '-封面图',
                'is_forever' => $materialModel->is_forever,
                'view_url' => $materialModel->thumb_view_url,
            ]);
            $materialModel->thumb_material_id = $thumbData['material_id'] ?? '';
            $materialModel->thumb_media_id = $thumbData['media_id'];
        }
        $materialModel->preview_data = [
            'title' => $materialModel->title,
            'description' => $materialModel->description,
            'music_url' => $materialModel->music_url,
            'hq_music_url' => $materialModel->hq_music_url,
            'thumb_view_url' => $materialModel->thumb_view_url,
        ];
        $materialModel->save();
        return $materialModel->toArray();
    }

    /**
     * 删除素材
     * @param $materialId
     */
    public function del($materialId)
    {
        $findMaterial = WxMaterial::where('appid', $this->appid)->findOrFail($materialId);
        return $findMaterial->delete();
    }

    /**
     * 获取被动回复数据
     * @param $materialId
     */
    public function getPassiveReplyData($materialId)
    {
        $findMaterial = WxMaterial::where('appid', $this->appid)->find($materialId);
        if ($findMaterial){
            //获取音乐缩略图临时素材ID
            $thumbService = new WxMaterialThumb();
            $thumb_media_id = $thumbService->getMediaId($findMaterial->thumb_material_id);
            return new Music([
                'title' => $findMaterial->title ?? '',
                'description' => $findMaterial->description ?? '',
                'url' => $findMaterial->music_url ?? '',
                'hq_url' => $findMaterial->hq_music_url ?? '',
                'thumb_media_id' => $thumb_media_id ?? '',
            ]);
        }else{
            return '';
        }
    }

    /**
     * 获取微信mediaId（临时素材和永久素材处理方式不一样）
     * @param $materialId 素材主键ID
     * @param $reUpload 是否重新上传
     * @return string 素材media_id
     */
    public function getMediaId($materialId, $reUpload = false)
    {
        throw new \Exception('音乐素材没有mediaId');
    }
    /**
     * 获取群发素材ID
     * @param $materialId
     * @param $reUpload
     */
    public function getMassMediaId($materialId, $reUpload = false)
    {
        throw new \Exception('不支持获取群发素材ID');
    }
    /**
     * 通过素材ID下载到本地
     * @param $mediaId
     * @param bool $isForever
     */
    public function downloadMediaId($mediaId, $isForever = true)
    {
        throw new \Exception('音乐素材不可下载');
    }

    /**
     * 同步线上数据到本地数据
     */
    public function sync()
    {
        throw new \Exception('音乐素材不可同步');
    }
}